package kr.hkit.powerjava.chther26.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;




import kr.hkit.powerjava.chther26.common.ConnectionFactory;
import kr.hkit.powerjava.chther26.common.DbUtil;
import kr.hkit.powerjava.chther26.dto.Employee;

public class EmployeeDAO implements DaoInterface<Employee> {
	private Connection connection;
	private PreparedStatement pstmt;

	@Override
	public void insertItem(Employee item) throws SQLException {
		String sql = "insert into employee values(?,?,?,?,?,?)";
		connection = ConnectionFactory.getConnection();
		pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		pstmt.setString(2, item.getEmpName());
		pstmt.setString(3, item.getTitle());
		pstmt.setInt(4, item.getManager());
		pstmt.setInt(5, item.getSalary());
		pstmt.setInt(6, item.getDno());
		
		
		pstmt.executeUpdate();
		DbUtil.close(pstmt);

	}

	@Override
	public void deleteItemByNo(Employee item) throws SQLException {
		String sql = "delete from employee where empno= ?";
		pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		pstmt.executeUpdate();
		DbUtil.close(pstmt);
	}

	@Override
	public void updateItemByNo(Employee item) throws SQLException {
		String sql = "update employee set manager=?,salary=? where empno=?"; //매니져 이름 변경
		pstmt = connection.prepareStatement(sql);
		pstmt.setInt(3, item.getEmpNo());
//		pstmt.setString(4, item.getTitle());
		pstmt.setInt(1, item.getManager());
		pstmt.setInt(2, item.getSalary());
//		pstmt.setInt(5, item.getDno());
		
		System.out.println(pstmt);
		pstmt.executeUpdate();
		DbUtil.close(pstmt);
	}

	@Override
	public ArrayList<Employee> listItems() throws SQLException {
		ArrayList<Employee> list = new ArrayList<>();
		String sql = "select empno,empname,title,manager,salary,dno from employee";
		pstmt = connection.prepareStatement(sql);
		ResultSet rs = pstmt.executeQuery();
		while (rs.next()) {
//			list.add(new Employee(rs.getInt("empNo"), rs
//					.getString("empname"), rs.getInt("empsalary")));
			list.add(new Employee(rs.getInt("empno"), 
					rs.getString("empname"),rs.getString("title"),rs.getInt("manager"),rs.getInt("salary"),rs.getInt("dno")));
		}

		DbUtil.close(rs);
		DbUtil.close(pstmt);
		return list;
	}

	
	public EmployeeDAO(Connection connection) {
		this.connection = connection;
	}

	@Override
	public Employee searchItem(Employee item) throws SQLException {
		Employee emp = null;
		String sql = "select empno,empname,title,manager,salary,dno from employee where empno=?";
		pstmt = connection.prepareStatement(sql);
		pstmt.setInt(1, item.getEmpNo());
		ResultSet rs = pstmt.executeQuery();
		if (rs.next()) {
			emp = new Employee();
			emp.setEmpNo(rs.getInt(1));
			emp.setEmpName(rs.getString(2));
			emp.setTitle(rs.getString(3));
			emp.setManager(rs.getInt(4));
			emp.setSalary(rs.getInt(5));
			emp.setDno(rs.getInt(6));
		}
		return emp;
	}
}
